/*
Copyright 2000-2011 Francois de Bertrand de Beuvron

This file is part of CoursBeuvron.

CoursBeuvron is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CoursBeuvron is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CoursBeuvron.  If not, see <http://www.gnu.org/licenses/>.
 */
package fr.insa.beuvron.tps.m2.dessin2DV3.gui;

import fr.insa.beuvron.tps.m2.dessin2DV3.Figure;
import fr.insa.beuvron.tps.m2.dessin2DV3.EnsembleFigure;
import fr.insa.beuvron.tps.m2.dessin2DV3.Point;
import fr.insa.beuvron.tps.m3.gui.TestePanel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author francois
 */
public class InterfacePartielle3 extends JPanel implements ActionListener {

    private JButton jbAfficheTexte;
    private JButton jbNouveauPoint;
    private SousFigsCombobox jcSousFigs;
    private JPanel jpBoutons;
    private JTextArea jtaDetail;
    private JScrollPane jspDetail;
    private EnsembleFigure fig;

    public InterfacePartielle3(EnsembleFigure fig) {
        this.fig = fig;

        this.setLayout(new BorderLayout());

        this.jpBoutons = new JPanel();
        this.jbAfficheTexte = new JButton("affiche tout");
        this.jbAfficheTexte.addActionListener(this);
        this.jpBoutons.add(this.jbAfficheTexte);
        this.jbNouveauPoint = new JButton("nouveau point");
        this.jbNouveauPoint.addActionListener(this);
        this.jpBoutons.add(this.jbNouveauPoint);
        this.jcSousFigs = new SousFigsCombobox(this.fig);
        this.jcSousFigs.addActionListener(this);
        this.jpBoutons.add(this.jcSousFigs);
        this.add(this.jpBoutons, BorderLayout.NORTH);

        this.jtaDetail = new JTextArea(10, 40);
        this.jspDetail = new JScrollPane(this.jtaDetail);
        this.add(this.jspDetail, BorderLayout.CENTER);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.jbNouveauPoint) {
            PointPanel2 editor = new PointPanel2();
            Point nouveau = new Point();
            editor.modifieEditeur(nouveau);
            if (JOptionPane.showConfirmDialog(this, editor, "coords point", JOptionPane.OK_CANCEL_OPTION)
                    == JOptionPane.OK_OPTION) {
                editor.modifieObjet(nouveau);
                this.fig.ajouteFigure(nouveau);
                this.jcSousFigs.miseAJour();
            }
        } else if (e.getSource() == this.jbAfficheTexte) {
            this.jtaDetail.setText(this.fig.jolieDescription(0));
        } else if (e.getSource() == this.jcSousFigs) {
            int selIndex = this.jcSousFigs.getSelectedIndex();
            if (selIndex != -1) {
                Figure selFig = (Figure) this.fig.getLesFigs().get(selIndex);
                this.jtaDetail.setText(selFig.jolieDescription(0) + "\n");
            }
        }
    }
    
    public static void main(String[] args) {
        EnsembleFigure figs = new EnsembleFigure();
        InterfacePartielle3 i1 = new InterfacePartielle3(figs);
        TestePanel.tailleFixee("interface demo1", i1, 600, 400);
    }
}
